#include "serial.h"

void setup_serial(void)
{
  Serial.begin(115200);
  DEBUG_LOG_printf("\r\n\r\n--------------------Program Start-----------------------\r\n");
  DEBUG_LOG_printf("*espressif 8266 esp12e Board\r\n");
  DEBUG_LOG_printf("*Firmware Version : " FirmwareVersions "\r\n");
  DEBUG_LOG_printf("*Firmware Build Date : %s\r\n", __DATE__);
  DEBUG_LOG_printf("*Firmware Build Time : %s\r\n", __TIME__);
  DEBUG_LOG_printf("*Firmware Build File : %s\r\n", __FILE__);
  DEBUG_LOG_printf("*Hardware Version : " HardwareVersions "\r\n");
  DEBUG_LOG_printf("--------------------------------------------------------\r\n\r\n");
  delay(100);
}

void loop_serial(void)
{
  static at_cmd_parse_t AT_parse_cmd_handler;
  static String rxBuffer = "\0";
  String tail = "\r\n";
  String cmd;
  int cutLen = 0;
  int strLen = 0;

  // DEBUG_LOG_print("SERIAL:");
  // DEBUG_LOG_println(millis());
  if (Serial.available() > 0)
  {
    rxBuffer = rxBuffer + (char)Serial.read();
    cutLen = rxBuffer.indexOf(tail);
    if (cutLen > 0)
    {
      // Serial.println(cutLen);
      strLen = rxBuffer.length();
      cmd = rxBuffer.substring(0, cutLen);
      rxBuffer = rxBuffer.substring(cutLen + tail.length(), strLen);

      AT_Execute_Handler(cmd, &AT_parse_cmd_handler);
    }
  }
}
